package com.ykzhujiang.lovespirit.parser;

import java.util.Calendar;
import java.util.Vector;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.ykzhujiang.lovespirit.entity.LThread;
import com.ykzhujiang.lovespirit.entity.Notice;
import com.ykzhujiang.lovespirit.util.Utility;

public abstract class Parser implements IParser {

	public LThread Parse(String url_text, String text) {
		if (text == null)
			return null;
		lthread = new LThread();
		Pattern p = Pattern.compile(notice_pattern);
		Matcher m = p.matcher(text);
		while (m.find()) {
			String text_single = m.group(1);
			Notice notice = ParseSingle(url_text, text_single);
			if (!notice.getTitle().trim().equals(""))
				lthread.notices.add(notice);
		}
		if (lthread.notices.size() == 0)
			return null;
		lthread.notices.get(0).setHot(lthread.notices.size());
		return lthread;
	}

	public Notice ParseSingle(String url_text, String text) {
		Notice notice = new Notice();
		ParseResource(text, notice);
		text = Utility.RemoveHTMLTag(text);
		ParseWriter(text, notice);
		ParseBoard(text, notice);
		ParseTitle(text, notice);
		ParseSite(text, notice);
		ParseDate(text, notice);
		ParseContent(text, notice);
		notice.setSourceUrl(url_text);
		return notice;

	}

	public int ParseResource(String text, Notice notice) {
		Pattern p = Pattern.compile(resource_pattern);
		Matcher m = p.matcher(text);
		while (m.find()) {
			String raw = resource_root + m.group(1);
			if (!notice.getResourceUrl().contains(raw)) {
				notice.getResourceUrl().add(raw);
			}
		}
		Vector urls = Utility.GetURLS(text);
		for (int i = 0; i < urls.size(); i++) {
			if (!notice.getResourceUrl().contains((String) urls.get(i))) {
				notice.getResourceUrl().add((String) urls.get(i));
			}
		}
		return 0;
	}

	public int ParseWriter(String text, Notice notice) {
		Pattern p = Pattern.compile(writer_pattern);
		Matcher m = p.matcher(text);
		if (m.find()) {
			notice.setWriter(m.group(1));
		}
		return 0;
	}

	public int ParseTitle(String text, Notice notice) {
		Pattern p = Pattern.compile(title_pattern);
		Matcher m = p.matcher(text);
		if (m.find()) {
			notice.setTitle(m.group(1));
		}
		return 0;
	}

	public int ParseBoard(String text, Notice notice) {
		Pattern p = Pattern.compile(board_pattern);
		Matcher m = p.matcher(text);
		if (m.find()) {
			notice.setBoard(m.group(1));
		}
		return 0;
	}

	public int ParseSite(String text, Notice notice) {
		Pattern p = Pattern.compile(site_pattern);
		Matcher m = p.matcher(text);
		if (m.find()) {
			notice.setSite(m.group(1));
		}
		return 0;
	}

	public int ParseDate(String text, Notice notice) {
		Pattern p = Pattern.compile(date_pattern);
		Matcher m = p.matcher(text);
		if (m.find()) {
			Calendar c = notice.getCalDate();
			c
					.set(Integer.parseInt(m.group(2)), Integer.parseInt(m
							.group(3)) - 1, Integer.parseInt(m.group(4)),
							Integer.parseInt(m.group(5)), Integer.parseInt(m
									.group(6)), Integer.parseInt(m.group(7)));
			notice.setCalDate(c);

		}
		return 0;
	}

	public int ParseContent(String text, Notice notice) {
		Pattern p = Pattern.compile(content_pattern);
		Matcher m = p.matcher(text);
		if (m.find()) {
			notice.setContent(m.group(2));
		}
		return 0;
	}

	public String resource_root;

	public String resource_pattern;

	public String writer_pattern;

	public String title_pattern;

	public String board_pattern;

	public String site_pattern;

	public String content_pattern;

	public String date_pattern;

	public String notice_pattern;

	LThread lthread;
}
